%This file creates vlag, v1mkt, v2mkt, vmksh, v1sys, v2sys, vsysh, vtipmsh
%(based on a different market definition: HRR) that will be merged back to 
%STATA for the reduced-form regression 


clear
tic


vndlist=1:13;
textFileName1=sprintf('hinfo_hrr.csv'); %input file: ahaid year prechs adj_vnd HRR sysid
B=dlmread(textFileName1);
B=sortrows(B,[1,2]);


textFileName11=sprintf('choice.csv'); %output file: the choice of vendors by each hospital
textFileName12=sprintf('vlag.csv'); %output file: whether each vendor was chosen in the last period
textFileName13=sprintf('v1mkt.csv'); %output file: whether each vendor is first market-dominant
textFileName14=sprintf('v2mkt.csv'); %output file: whether each vendor is second market-dominant (for tipping behavior)
textFileName15=sprintf('vmksh.csv'); %output file: the market share of each vendor
textFileName16=sprintf('vtipmsh.csv'); %output file: the difference of market share to the first-dominant vendor's share
textFileName17=sprintf('v1sys.csv'); %output file: whether each vendor is first system-dominant
textFileName18=sprintf('v2sys.csv'); %output file: whether each vendor is second system-dominant
textFileName19=sprintf('vsysh.csv'); %output file: the "share" each vendor by system members

textFileName13e=sprintf('exov1mkt.csv'); %output file: (exogenous) whether each vendor is first market-dominant
textFileName14e=sprintf('exov2mkt.csv'); %output file: (exogenous) whether each vendor is second market-dominant (for tipping behavior)
textFileName15e=sprintf('exovmksh.csv'); %output file: (exogenous) the market share of each vendor
textFileName16e=sprintf('exovtipmsh.csv'); %output file: (exogenous) the difference of market share to the first-dominant vendor's share
textFileName17e=sprintf('exov1sys.csv'); %output file: (exogenous) whether each vendor is first system-dominant
textFileName18e=sprintf('exov2sys.csv'); %output file: (exogenous) whether each vendor is second system-dominant
textFileName19e=sprintf('exovsysh.csv'); %output file: (exogenous) the "share" each vendor by system members


chs=zeros(size(B,1),numel(vndlist));
vlag=zeros(size(B,1),numel(vndlist));
v1mkt=zeros(size(B,1),numel(vndlist));
v2mkt=zeros(size(B,1),numel(vndlist));
vmksh=zeros(size(B,1),numel(vndlist));
%vtipmsh=zeros(size(B,1),numel(vndlist));
v1sys=zeros(size(B,1),numel(vndlist));
v2sys=zeros(size(B,1),numel(vndlist));
vsysh=zeros(size(B,1),numel(vndlist));


exov1mkt=zeros(size(B,1),numel(vndlist));
exov2mkt=zeros(size(B,1),numel(vndlist));
exovmksh=zeros(size(B,1),numel(vndlist));
%exovtipmsh=zeros(size(B,1),numel(vndlist));
exov1sys=zeros(size(B,1),numel(vndlist));
exov2sys=zeros(size(B,1),numel(vndlist));
exovsysh=zeros(size(B,1),numel(vndlist));
%}


isv1mkt=zeros(size(B,1),1);
isv1sys=zeros(size(B,1),1);
exoisv1mkt=zeros(size(B,1),1);
exoisv1sys=zeros(size(B,1),1);


%endogenous
for i=1:size(B,1)
    vlag(i,:)=ismember(vndlist,B(i,3));
    chs(i,:)=ismember(vndlist,B(i,4));
    %compute market-related
    ms=B(B(:,5)==B(i,5)&B(:,2)==B(i,2),3);
    ms(ms==1)=[];
    if isempty(ms)==0
        occur=histc(ms,vndlist);
        occur=reshape(occur,1,[]);  
        vmksh(i,:)=occur/sum(occur);
        ms(ms==2)=[];
        ms(ms==13)=[];
        if isempty(ms)==0
          occur=histc(ms,vndlist);
          occur=reshape(occur,1,[]);      

        
          v1mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
          isv1mkt(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
          %v1mkt(i,2)=0;
          %v1mkt(i,13)=0;
          %vmksh(i,:)=occur/sum(occur);
          occur(occur==max(occur))=0;
          if max(occur)>0
             v2mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
             %v2mkt(i,2)=0;
             %v2mkt(i,13)=0;           
          end  
        end
    end
    
   %compute system-related
   if B(i,6)>0
      sys=B(B(:,6)==B(i,6)&B(:,2)==B(i,2),3);
      sys(sys==1)=[];
      if isempty(sys)==0
         occur=histc(sys,vndlist);
         occur=reshape(occur,1,[]);
         v1sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
         isv1sys(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
         %v1sys(i,13)=0; 
         vsysh(i,:)=occur/sum(occur);
         occur(occur==max(occur))=0;
         if max(occur)>0
             v2sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
             %v2sys(i,13)=0;           
         end               
      end
   end   
end
vtipmsh=repmat(max(vmksh,[],2),1,size(vmksh,2))-vmksh;
vlag(:,1)=0;



%exogenous
for i=1:size(B,1)
    %compute market-related
    ms=B(B(:,5)==B(i,5)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),3);
    ms(ms==1)=[];
    if isempty(ms)==0
        occur=histc(ms,vndlist);
        occur=reshape(occur,1,[]);
        exovmksh(i,:)=occur/sum(occur);
        ms(ms==2)=[];
        ms(ms==13)=[];
      if isempty(ms)==0;
        occur=histc(ms,vndlist);
        occur=reshape(occur,1,[]);
        exov1mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
        exoisv1mkt(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
        %exov1mkt(i,2)=0;
        %exov1mkt(i,13)=0;
        %exovmksh(i,:)=occur/sum(occur);
        occur(occur==max(occur))=0;
        if max(occur)>0
           exov2mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
           exov2mkt(i,2)=0;
           exov2mkt(i,13)=0;           
        end 
      end
    end
    
   %compute system-related
   if B(i,6)>0
      sys=B(B(:,6)==B(i,6)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),3);
      sys(sys==1)=[];
      if isempty(sys)==0
         occur=histc(sys,vndlist);
         occur=reshape(occur,1,[]);
         exov1sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
         exoisv1sys(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
         %exov1sys(i,13)=0; 
         exovsysh(i,:)=occur/sum(occur);
         occur(occur==max(occur))=0;
         if max(occur)>0
             exov2sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
             %exov2sys(i,13)=0;           
         end               
      end
   end   
end
exovtipmsh=repmat(max(exovmksh,[],2),1,size(exovmksh,2))-exovmksh;

%}


dlmwrite(textFileName11,[chs,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName12,[vlag,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName13,[v1mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName14,[v2mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName15,[vmksh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName16,[vtipmsh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName17,[v1sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName18,[v2sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName19,[vsysh,B(:,1:2)],'delimiter', ',', 'precision', 8)    





dlmwrite(textFileName13e,[exov1mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName14e,[exov2mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName15e,[exovmksh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName16e,[exovtipmsh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName17e,[exov1sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName18e,[exov2sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName19e,[exovsysh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
%}










toc



